var rootPath = getRootPath();
var oldUname = '';

$(function() {
	focusById();
	createProvSelect(document.getElementById("province"));
	
	$.ajax({
		url : rootPath + "/user/queryUserself",
		method : "GET",
		success : function(data) {
			if (data.returnCode == '0000') {
				if (data.returnObject != null) {
					var obj = data.returnObject;
					oldUname = obj.uname;
					if (obj.uname != null && obj.uname != "") {
						$("#uname").val(obj.uname);
					}
					if (obj.sex != null && obj.sex != "") {
						$("input:radio[name=sex][value='" + obj.sex + "']")
								.attr("checked", true);
					}
					if (obj.birthday != null && obj.birthday != "") {
						$("#birthday").val(obj.birthday);
					}
					if (obj.province != null && obj.province != "") {
						$("#province option[value='" + obj.province + "']")
								.prop("selected", "selected");
						$("#province").change();
					}
					if (obj.city != null && obj.city != "") {
						$("#city option[value='" + obj.city + "']").prop(
								"selected", "selected");
						$("#city").change();
					}
					if (obj.area != null && obj.area != "") {
						$("#area option[value='" + obj.area + "']").prop(
								"selected", "selected");
					}
					if (obj.intro != null && obj.intro != "") {
						$("#intro").val(obj.intro);
						var val = $('#intro').val();
						var len = val.replace(/\s*/g,"");//去掉空格
						$('#text-count').text(len.length);
					}
					
					var roles = obj.roles;
					if(roles){
						roles.map(function(item,index){
							//学校账号  不能修改昵称
							if(item.rleId && item.rleId == "20181529574197910125") {
								$("#uname").attr("disabled", "true");
							}
						});
					}
				}
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error");
			window.parent.location.href = "/account";
		}
	});

	$.ajax({
		url : rootPath + '/verify/dr/1/uid',
		type : 'GET',
		contentType : 'application/json;charset=UTF-8',
		success : function(data) {
			if (data.returnCode == '0000') {
				var object = data.returnObject;
				if (object != null && object.length > 0 && object[0].sort > 100
						&& object[0].state == 2) {
					// 若已进行官方认证,且官方认证已通过,不能修改昵称
					$("#uname").attr("disabled", "true");
				}
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error");
		}
	});

	/* 字数限制 */
	$('#intro').bind("change keydown up", function() {
		$(this).val($(this).val().substring(0, 200));
		$('#text-count').text($("#intro").val().length);
	});
});

function checkData() {
	if ($("#uname").val() == '') {
		$("#uname").focus();
		showErrMsg("uname", "");
		
		return false;
	}
	return true;
}
function showErrMsg(id, message) {
	if (message != '') {
		$("#" + id + "Msg2").text(message);
	}
	$("#" + id + "Msg").show();
}
function hideMsg(id) {
	$("#" + id + "Msg").hide();
}
function focusById() {
	$("#uname").focus(function() {
		hideMsg("uname");
		flag = true;
	});
}